package com.example.helllo;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.SimpleAdapter;

import java.util.ArrayList;
import java.util.HashMap;

public class MyListActivity2 extends AppCompatActivity implements AdapterView.OnItemClickListener{
    private static final String TAG = "MyListActivity2";
    ListView listView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_list2);
        listView = findViewById(R.id.mylist2);
        ProgressBar progressBar=findViewById(R.id.progressBar);
        progressBar.setVisibility(View.GONE);
        ArrayList<HashMap<String,String>> listItems = new ArrayList<HashMap<String,String>>();//存放信息
        for(int i=0;i<10;i++) {
            HashMap<String, String> map = new HashMap<String, String>();
            map.put("ItemTitle", "Rate:" + i);//标题，前面ItemTitle是key,后面"Rate:" + i是值。
            map.put("ItemDetail:", "detail" + i);//详情，key不能重复
            listItems.add(map);//把map添加到列表中
        }
        //生成适配器的item 和动态数组对应的元素
//        SimpleAdapter listItemAdapter=new SimpleAdapter(this,
//                listItems,//数据源
//                R.layout.list_item,//布局
//                new String[]{"ItemTitle","ItemDetail"},
//                new int[]{R.id.itemTitle,R.id.itemDetail}//布局控件和数据之间的一一对应关系
//                );
//        listView.setAdapter(listItemAdapter);
        listView.setOnItemClickListener(this);
        //获取网络数据
        Handler handler=new Handler(){
            @Override
            public void handleMessage(@NonNull Message msg) {
                if(msg.what==7){
                    //进行消息处理
                    //ArrayList<String> list2=(ArrayList<String>)msg.obj;

                    //ListAdapter adapter=new ArrayAdapter<String>(FirstListActivity.this, android.R.layout.simple_list_item_1,list2);
                    //setListAdapter(adapter);//设置数据适配器，把当前界面用adapter绑定
                    ArrayList<HashMap<String,String>>listItems = (ArrayList<HashMap<String,String>>)msg.obj;
                    SimpleAdapter listItemAdapter=new SimpleAdapter(MyListActivity2.this,
                            listItems,
                            R.layout.list_item,
                            new String[]{"Country","Rate"},
                            new int[]{R.id.itemTitle,R.id.itemDetail}
                    );
                    MyAdapter adapter=new MyAdapter(MyListActivity2.this,R.layout.list_item,listItems);
                    listView.setAdapter(adapter);
                    //listView.setAdapter(listItemAdapter);
                    //setListAdapter(listItemAdapter);
                    listView.setVisibility(View.VISIBLE);

                }
                super.handleMessage(msg);
            }
        };
        MyTask task=new MyTask();
        task.setHandler(handler);
        Thread t=new Thread(task);
        t.start();
    }

    @Override
    //监听
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        //获得当前行对应的对象
        Object itemAtPosition=listView.getItemAtPosition(position);
        HashMap<String,String> map=(HashMap<String,String>) itemAtPosition;
        //点击行获取数据
        String countryStr=map.get("Country");
        String rateStr=map.get("Rate");
        Log.i(TAG,"onItemClick:countryStr"+countryStr);
        Log.i(TAG,"onItemClick:rateStr"+rateStr);
        //启动子线程
        Intent config = new Intent(this, CountRateActivity.class);
        config.putExtra("count_country", countryStr);
        config.putExtra("count_rate", rateStr);
        Log.i(TAG, "openCountRate:count_country=" + countryStr);
        Log.i(TAG, "openCountRate:count_rate=" + rateStr);
        //Log.i(TAG,"openConfig:dollarRate:",dollarRate);
        startActivityForResult(config, 8);
    }
}